module CharactersHelper
  def player_character
    player = current_player
    return nil unless player
    return player.player_character
  end
  
  def in_court? (character, court = player_character.court_territory)
    return false unless character && court
    ch_court = character.court_territory
    return false unless ch_court
    return court.id == ch_court.id && court.instance_of?(ch_court.class)
  end
  
  def is_vassal? (character)
    return false unless character
    liege = character.liege
    return false unless liege
    pc = player_character
    return false unless pc
    return liege.id == pc.id
  end
  
  def options_single_characters (sex, selected = 0)
    options_characters_select Character.find_all_by_sex_and_marital_status(sex, "single"), selected
  end
  
  def options_character_courtier (court, selected = "")
    return unless court
    options_characters_select(Character.find_all_courtiers(court), selected)  
  end
  
  def options_character_sex (selected = "")
    options_array_select Character.sexes, selected
  end
  
  def options_character_marital_status (selected = "")
    options_array_select Character.marital_status, selected
  end
  
  def options_characters_select (characters = Character.find(:all), selected = 0)
    options_records_select characters, selected
  end
  
  def options_male_characters (selected = 0)
    options_characters_select Character.find_all_by_sex("male"), selected
  end
  
  def options_female_characters (selected = 0)
    options_characters_select Character.find_all_by_sex("female"), selected
  end
  
  def link_to_father (character)
    if character && character.father
      father = Character.find(character.father) unless character.father < 1
    end
    if father
      link_to "#{father.christian_name} #{father.family_name}", father
    else
      "None"
    end
  end
  
  def link_to_mother (character)
    if character && character.mother
      mother = Character.find(character.mother) unless character.mother < 1
    end
    if mother
      link_to "#{mother.christian_name} #{mother.family_name}", mother
    else
      "None"
    end
  end
end
